package com.wutong.teacher_server.utils;


import com.baidu.aip.face.AipFace;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;

public class faceApiUtil {
    //设置APPID/AK/SK
    public static final String APP_ID = "23578982";
    public static final String API_KEY = "8qPbbuxyTg3fwk7fm4CZsbBH";
    public static final String SECRET_KEY = "kMr2RvIxTlWCO91CaEePFmGrPuU4MGk0";

    //初始化客户端
    public static AipFace createClient(){
        // 初始化一个AipFace
        AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
        // 可选：设置网络连接参数
        //建立连接的超时时间（单位：毫秒）
        client.setConnectionTimeoutInMillis(2000);
        //通过打开的连接传输数据的超时时间（单位：毫秒）
        client.setSocketTimeoutInMillis(60000);
        return client;
    }
    //人脸上传人脸库
    public static void sample(AipFace client,String imageBase) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("face_field","beauty");
        options.put("face_field","landmark");
        options.put("face_field","quality");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "LOW");

        String image = imageBase;
        String imageType = "BASE64";

        // 人脸检测
        JSONObject res = client.detect(image, imageType, options);
        String error_msg = res.getString("error_msg");
        int error_code = res.getInt("error_code");
        if("SUCCESS".equals(error_msg) && 0 == error_code){
            JSONObject result = (JSONObject) res.get("result");
            JSONArray face_list = (JSONArray) result.get("face_list");
            JSONObject r = (JSONObject)face_list.get(0);
            String face_token = r.getString("face_token");
            JSONObject quality = (JSONObject) r.get("quality");
            //脸部光照的灰度值(0~255),最低应大于40
            int illumination = quality.getInt("illumination");
            //模糊度范围(0~1），0是最清晰，1是最模糊,应小于0.7
            double blur = quality.getDouble("blur");
            //遮挡范围(0~1），0为无遮挡，1是完全遮挡
            JSONObject occlusion = (JSONObject) quality.get("occlusion");
            //左眼遮挡,阈值最大为0.6
            double left_eye = occlusion.getDouble("left_eye");
            //右眼遮挡,阈值最大为0.6
            double right_eye = occlusion.getDouble("right_eye");
            //左脸颊被遮挡,阈值为0.8
            double left_cheek = occlusion.getDouble("left_cheek");
            //右脸颊被遮挡,阈值为0.8
            double right_cheek = occlusion.getDouble("right_cheek");
            //鼻子被遮挡,阈值为0.7
            double nose = occlusion.getDouble("nose");
            //嘴巴被遮挡,阈值为0.7
            double mouth = occlusion.getDouble("mouth");
            //下巴被遮挡,阈值为0.6
            double chin_contour = occlusion.getDouble("chin_contour");


            System.out.println(res.toString(2));
        }else{

        }



    }
}
